package net.esj.basic.dao.querybuilder;



import java.util.NoSuchElementException;

import org.springframework.util.Assert;

public enum OrderExpression {
	
	DESC("desc"),
	ASC("asc");

	final String key;

	private OrderExpression(String key) {
		this.key = key;
	}
	
	public String getKey(){
		return key;
	}

	public static OrderExpression getValue(String key){
		for(OrderExpression se :OrderExpression.values()){
			if(se.getKey().equals(key)){
				return se;
			}
		}
		return null;
	}
	
	public static OrderExpression getValueWithException(String key) throws NoSuchElementException{
		for(OrderExpression se :OrderExpression.values()){
			if(se.getKey().equals(key)){
				return se;
			}
		}
		throw new NoSuchElementException("找不到该表达式");
	}
	
	public static void append(QueryBuilder qb,OrderExpression oe,String propertyName){
		Assert.notNull(oe);
		Assert.notNull(propertyName);
		switch(oe){
		case ASC:
			qb.OrderByAsc(propertyName);
			break;
		case DESC:
			qb.OrderByDesc(propertyName);
			break;
		}
	}
	
	public static void append(QueryBuilder qb,String expression,String propertyName){
		OrderExpression oe = OrderExpression.getValue(expression);
		append(qb, oe, propertyName);
	}
}
